home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / delphi1 / lesson2 / conv1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-12-06  |  2.2 KB  |  64 lines

  1. unit Conv1;
  2.  
  3. interface
  4.  
  5. uses
  6. { Delphi needs some procedures stored in the files or 'units' listed here.
  7. We'll be coming back to units later in this series . }
  8.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  9.   Forms, Dialogs, StdCtrls;
  10.  
  11. type
  12. { Delphi automatically generates this Type description when you design a
  13. form. Delphi uses an object orientated version of the Pascal language. Objects,
  14. such as forms, are defined as 'classes'. A new form inherits the features
  15. of Delphi's basic form class, TForm. Don't worry if you don't follow this.
  16. The good news is that you don't need to understand the details of object
  17. orientation to program in Delphi. Just let Delphi write these class
  18. definitions for you. }
  19.   TForm1 = class(TForm)
  20.     ConvertFromEdBox: TEdit;
  21.     ConvertToEdBox: TEdit;
  22.     Label1: TLabel;
  23.     Label2: TLabel;
  24.     CalcBtn: TButton;
  25.     procedure CalcBtnClick(Sender: TObject);
  26.   private
  27.     { Private declarations }
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;    { Our Form, Form1 is an object of the }
  34.                     { TForm1 type, defined above.         }
  35.  
  36. implementation
  37.  
  38. {$R *.DFM}
  39.  
  40. procedure TForm1.CalcBtnClick(Sender: TObject);
  41. { This code is attached to the CalcBtn object and is executed when the }
  42. { click event occurs.                                                  }
  43. const
  44. { This is the miles to kilometres conversion factor                    }
  45.      mileToKM = 1.60934;
  46. var
  47.    InputVal, OutputVal : Real;
  48.    Code : integer;
  49.    S : string;
  50. begin
  51. { First convert the text entered into the ConvertFromEdBox field to a Real }
  52. { value. The variable, Code, can be used for error checking. But we'll     }
  53. { ignore it for the time being.                                            }
  54.    Val(ConvertFromEdBox.Text, InputVal, Code );
  55. { Do the calculation to convert miles to kilometres                        }
  56.    OutputVal := InputVal * mileToKM;
  57. { Now convert the result, OutPutVal to a string.                           }
  58.    Str( OutputVal:2:2, S );
  59. { Display the string in the ConvertToEdBox field.                          }
  60.    ConvertToEdBox.Text :=  S;
  61. end;
  62.  
  63. end.
  64.